package com.xiaoshangkou.hellopra.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.mapper.PostMapper;
import com.xiaoshangkou.hellopra.pojo.Category;
import com.xiaoshangkou.hellopra.pojo.Member;
import com.xiaoshangkou.hellopra.pojo.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostService {
    @Autowired
    private PostMapper postMapper;

    //分页查询所有帖子
    public Page<Post> findPage(int page,int size){
        return postMapper.selectPage(new Page<>(page, size), null);
    }

    public List<Post> findAll(){
        return postMapper.findAllPost();

    }
    //启用/禁用用户
    public void updateStatus(Integer pid){
        Post post = postMapper.selectById(pid);
        post.setStatus(!post.isStatus());//取反
        postMapper.updateById(post);
    }

    //根据名称查询用户(模糊查询)
    public Page<Post> findPageByTitle(int page, int size, String title){
        //构建一个查询的wrapper
        QueryWrapper<Post> wrapper = new QueryWrapper();

        wrapper.like(StringUtils.isNotBlank(title),"postTitle",title);
        return postMapper.selectPage(new Page<>(page, size), wrapper);
    }

    public List<Post> findByTitle(String title){
        return postMapper.findByTitle("%"+title+"%");

    }

    //查询生活趣事版块所有帖子
    public List<Post> findAllLife(Integer cid){
        return postMapper.findAllLife(cid);
    }

    //查询生活趣事版块所有帖子
    public Page<Post> findLifePost(Integer cid,int page,int size){
        QueryWrapper<Post> queryWrapper=new QueryWrapper<>();
        if (cid!=null){
            queryWrapper.eq("cid",cid);
        }
        //审核通过的帖子
        queryWrapper.eq("status",1);
        //倒叙排列
        queryWrapper.orderByAsc("status");

        return postMapper.selectPage(new Page(page,size),queryWrapper);
    }

    //添加新帖子
    public void add(Post post){
        postMapper.insert(post);
    }

    //根据pid查询帖子
    public Post findByPid(Integer pid){
        Post postByPid = postMapper.findPostByPid(pid);
        return postByPid;
    }

}
